#ifndef _STDTCPSERVER_H_
#define _STDTCPSERVER_H_

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <cctype>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <memory>

/*类型别名*/
class StdTcpSocket;
using StdTcpSocketPtr = std::shared_ptr<StdTcpSocket>;

struct stdTcpServerPrivate
{
    /*监听的句柄*/
    int m_sockfd;
    /*服务器监听状态标识符*/
    bool m_isRunning;
};

struct stdTcpSocketPrivate
{
    /*通信句柄*/
    int m_connfd;

    /*通信状态*/
    bool m_connected;
};

/*通信类*/
class StdTcpSocket
{
public:
    StdTcpSocket();

    ~StdTcpSocket();

public:
    /*连接服务器*/
    int connectToServer(const char* ip, int port);
    /*是否连接成功*/
    bool isConnected() const;

    /*发送数据*/
    int sendMessage(const std::string & sendMsg);
    int sendMessage(const void* sendMsg, size_t n);

    /*接受数据*/
    int recvMessage(std::string & recvMsg);
    int recvMessage(void *buf, size_t n);

    /*设置属性*/
    stdTcpSocketPrivate* getSockAttr() const;

private:
    std::unique_ptr<stdTcpSocketPrivate> m_sockAttr;
};

/*服务器类*/
class StdTcpServer
{
public:
    /*构造函数*/
    StdTcpServer();

    /*析构函数*/
    ~StdTcpServer();

public:
    /*设置监听*/
    bool setListen(int port);

    /*接受客户端的连接*/
    std::shared_ptr<StdTcpSocket> getClientSock();

private:
    /*成员变量*/

    /*端口*/
    int m_port;
    
    /*智能指针 --- 服务器属性*/
    std::unique_ptr<stdTcpServerPrivate> m_tcpAttr;
};

#endif //_STDTCPSERVE_H_